package com.cq.hd.merchant.controller;

import com.cq.hd.common.annotation.MerchantLoginUser;
import com.cq.hd.common.entity.MerchantAppLoginUser;
import com.cq.hd.common.enums.biz.ActiveTypeEnum;
import com.cq.hd.common.pagination.Page;
import com.cq.hd.common.response.Result;
import com.cq.hd.common.response.ResultGenerator;
import com.cq.hd.common.utils.StringUtils;
import com.cq.hd.merchant.biz.OrderBiz;
import com.cq.hd.order.api.TbOrderApi;
import com.cq.hd.order.api.dto.*;
import com.cq.hd.order.api.vo.*;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiResponse;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

@Slf4j
@RestController
@RequestMapping("/order")
@Api(tags = "订单相关接口")
public class OrderController {

    @Autowired
    private TbOrderApi orderApi;
    @Resource
    private OrderBiz orderBiz;

    @ApiOperation(value = "订单列表")
    @ApiResponse(response = MerchantOrderPageVo.class, code = 200, message = "接口返回对象参数")
    @GetMapping("/page")
    public Result<Page<MerchantOrderPageVo>> page(MerchantOrderPageDto merchantOrderPageDto,
                                                  @ApiIgnore @MerchantLoginUser MerchantAppLoginUser merchantAppLoginUser) {
        merchantOrderPageDto.setMerchantId(merchantAppLoginUser.getMerchantId());
        merchantOrderPageDto.setOrderType(null);
        return orderApi.pageMerchantOrder(merchantOrderPageDto);
    }

    @ApiOperation(value = "订单导出")
    @GetMapping("/export")
    public void export(MerchantOrderExportDto merchantOrderExportDto,
                       @ApiIgnore @MerchantLoginUser MerchantAppLoginUser merchantAppLoginUser, HttpServletResponse response) {
        merchantOrderExportDto.setMerchantId(merchantAppLoginUser.getMerchantId());
        if (StringUtils.isNotBlank(merchantOrderExportDto.getBusinessIdsStr())) {
            List<Long> businessIds = Arrays.stream(merchantOrderExportDto.getBusinessIdsStr().split(","))
                    .map(Long::valueOf).collect(Collectors.toList());
            merchantOrderExportDto.setBusinessIds(businessIds);
        }
        orderBiz.export(merchantOrderExportDto, response);
    }

    @ApiOperation(value = "订单详情")
    @ApiImplicitParam(name = "id", value = "id", paramType = "query", dataType = "Long")
    @ApiResponse(response = MerchantOrderDetailVo.class, code = 200, message = "接口返回对象参数")
    @GetMapping("/detail")
    public Result<MerchantOrderDetailVo> detail(Long id, @ApiIgnore @MerchantLoginUser MerchantAppLoginUser merchantAppLoginUser) {
        return orderApi.detailMerchantOrder(id, merchantAppLoginUser.getMerchantId());
    }

    @ApiOperation(value = "核销确认(根据核销码获取订单确认数据)")
    @ApiImplicitParam(name = "verifyCode", value = "核销码", paramType = "query", dataType = "String")
    @ApiResponse(response = MerchantVerifyConfirmVo.class, code = 200, message = "接口返回对象参数")
    @GetMapping("/getByVerifyCode")
    public Result<MerchantVerifyConfirmVo> getByVerifyCode(String verifyCode,
                                                           @ApiIgnore @MerchantLoginUser MerchantAppLoginUser merchantAppLoginUser) {
        return orderApi.getByVerifyCode(verifyCode, merchantAppLoginUser.getMerchantId());
    }

    @ApiOperation(value = "确认核销")
    @ApiImplicitParam(name = "merchantVerifyDto", value = "实体", paramType = "body", dataType = "MerchantVerifyDto")
    @PostMapping("/verify")
    public Result<Boolean> verify(@RequestBody MerchantVerifyDto merchantVerifyDto,
                                  @ApiIgnore @MerchantLoginUser MerchantAppLoginUser merchantAppLoginUser) {
        merchantVerifyDto.setMerchantId(merchantAppLoginUser.getMerchantId());
        return orderApi.verify(merchantVerifyDto);
    }

    @ApiOperation(value = "核销记录列表: 商品")
    @ApiResponse(response = MerchantVerifyGoodsPageVo.class, code = 200, message = "接口返回对象参数")
    @GetMapping("/pageVerifyGoods")
    public Result<Page<MerchantVerifyGoodsPageVo>> pageVerifyGoods(MerchantVerifyGoodsPageDto merchantVerifyGoodsPageDto,
                                                                   @ApiIgnore @MerchantLoginUser MerchantAppLoginUser merchantAppLoginUser) {
        merchantVerifyGoodsPageDto.setMerchantId(merchantAppLoginUser.getMerchantId());
        return orderApi.pageVerifyGoods(merchantVerifyGoodsPageDto);
    }

    @ApiOperation(value = "核销记录: 商品核销记录明细")
    @ApiResponse(response = MerchantVerifyGoodsPageVo.class, code = 200, message = "接口返回对象参数")
    @GetMapping("/pageVerifyGoodsDetail")
    public Result<Page<MerchantVerifyGoodsDetailPageVo>> pageVerifyGoodsDetail(MerchantVerifyGoodsDetailPageDto merchantVerifyGoodsDetailPageDto,
                                                                   @ApiIgnore @MerchantLoginUser MerchantAppLoginUser merchantAppLoginUser) {
        merchantVerifyGoodsDetailPageDto.setMerchantId(merchantAppLoginUser.getMerchantId());
        return orderApi.pageVerifyGoodsDetail(merchantVerifyGoodsDetailPageDto);
    }

    @ApiOperation(value = "核销记录列表: 签到码/积分订单")
    @ApiResponse(response = MerchantVerifyRecordPageVo.class, code = 200, message = "接口返回对象参数")
    @GetMapping("/pageVerifyRecord")
    public Result<Page<MerchantVerifyRecordPageVo>> pageVerifyRecord(MerchantVerifyRecordPageDto merchantVerifyRecordPageDto,
                                                                     @ApiIgnore @MerchantLoginUser MerchantAppLoginUser merchantAppLoginUser) {
        merchantVerifyRecordPageDto.setMerchantId(merchantAppLoginUser.getMerchantId());
        List<Integer> orderTypes = new ArrayList<>();
        if (ActiveTypeEnum.POINTS.getValue().equals(merchantVerifyRecordPageDto.getOrderType())) {
            orderTypes.add(ActiveTypeEnum.POINTS.getValue());
        } else {
            orderTypes.add(ActiveTypeEnum.ENROLL.getValue());
            orderTypes.add(ActiveTypeEnum.GROUP_ENROLL.getValue());
        }
        merchantVerifyRecordPageDto.setOrderTypes(orderTypes);
        return orderApi.pageVerifyRecord(merchantVerifyRecordPageDto);
    }

    @ApiOperation(value = "打印报名订单报名信息")
    @ApiResponse(response = Boolean.class, code = 200, message = "接口返回对象参数")
    @GetMapping("/printEnrollInfo")
    public Result<Boolean> printEnrollInfo(String verifyCode,
                                           @ApiIgnore @MerchantLoginUser MerchantAppLoginUser merchantAppLoginUser) {
        Boolean result = orderBiz.printEnrollInfo(verifyCode, merchantAppLoginUser);
        return ResultGenerator.success(result);
    }

}
